package org.noote.libs.roby.comm;

import org.noote.libs.graphicbuffer.GraphicBuffer;
import org.noote.libs.roby.configuration.PositionMarker;
import org.noote.libs.roby.scan.ScanResult;

public class IPServerCommandProtocol {

	//////////////////////////////////
	///
	/// Server class for IP communication
	///
	/////////////////////////////////
	static final String CMD_START_TRAME = "#";
	static final String CMD_END_TRAME = "$";
	protected static String _bounding(String sCmd, String sOp, String sParams)
	{
		return CMD_START_TRAME+sCmd+((sOp.length()>0)?"_"+sOp:"")+":"+sParams+CMD_END_TRAME+"\n";
	}

	/////////////////////////////////
	// Version
	static final String CMD_QUIT = "quit";
	public static String sendQuit()
	{
		return _bounding(CMD_QUIT, "", "");
	}

	/////////////////////////////////
	// Version
	static final String CMD_VERSION = "version";
	public static String sendHello(String sVersion, String sControllerName)
	{
		return _bounding(CMD_VERSION, "hello", "version="+sVersion+",name="+sControllerName);
	}
	public static String sendVersion(String sVersion, String sControllerName)
	{
		return _bounding(CMD_VERSION, "", "version="+sVersion+",name="+sControllerName);
	}

	/////////////////////////////////
	// settings...
	static final String CMD_SETTING = "setting";
	public static String sendSettings(boolean bReport_Transmission)
	{
		return _bounding(CMD_SETTING, "", "send_status="+(bReport_Transmission?"1":"0"));
	}
	public static String sendBodySettings(int iBody_MinSpeed, int iBody_MidSpeed, int iBody_MaxSpeed, int iBody_MinDist, int iBody_MaxDist)
	{
		return _bounding(CMD_SETTING, "body", "min_speed="+Integer.toString(iBody_MinSpeed)+",mid_speed="+Integer.toString(iBody_MidSpeed)+",max_speed="+Integer.toString(iBody_MaxSpeed)+",min_dist="+Integer.toString(iBody_MinDist)+",max_dist="+Integer.toString(iBody_MaxDist));
	}
	public static String sendTurretSettings(int iTurret_MinLow, int iTurret_MidLow, int iTurret_MaxLow, int iTurret_MinHigh, int iTurret_MidHigh, int iTurret_MaxHigh, int iTurret_MinDist, int iTurret_MidDist, int iTurret_MaxDist)
	{
		return _bounding(CMD_SETTING, "turret", "min_low="+Integer.toString(iTurret_MinLow)+",mid_low="+Integer.toString(iTurret_MidLow)+",max_low="+Integer.toString(iTurret_MaxLow)+",min_high="+Integer.toString(iTurret_MinHigh)+",mid_high="+Integer.toString(iTurret_MidHigh)+",max_high="+Integer.toString(iTurret_MaxHigh)+",min_dist="+Integer.toString(iTurret_MinDist)+",mid_dist="+Integer.toString(iTurret_MidDist)+",max_dist="+Integer.toString(iTurret_MaxDist));
	}
	public static String sendRaySettings(int iRay_Count, int iRay_Delay, int iRay_Sensors, float fRay_Precision)
	{
		return _bounding(CMD_SETTING, "ray", "count="+Integer.toString(iRay_Count)+",delay="+Integer.toString(iRay_Delay)+",sensors="+Integer.toString(iRay_Sensors)+",precision="+Float.toString(fRay_Precision));
	}
	
	/////////////////////////////////
	// hit...
	static final String CMD_HIT = "hit";
	public static String sendHitSensors(boolean bBody_Switch_BackLeft, boolean bBody_Switch_BackCenter, boolean bBody_Switch_BackRight)
	{
		return _bounding(CMD_HIT, "sensors", "bl="+(bBody_Switch_BackLeft?"1":"0")+",bc="+(bBody_Switch_BackCenter?"1":"0")+",br="+(bBody_Switch_BackRight?"1":"0"));
	}
	
	/////////////////////////////////
	// latest...
	static final String CMD_LATEST = "latest";
	public static String sendLatestDistance(float fBody_LastMoveDistance_Left, float _fBody_LastMoveDistance_Right)
	{
		return _bounding(CMD_LATEST, "distance", "left="+Float.toString(fBody_LastMoveDistance_Left)+",right="+Float.toString(_fBody_LastMoveDistance_Right));
	}
	
	/////////////////////////////////
	// turret...
	static final String CMD_TURRET = "turret";
	public static String sendTurretInformation(int iTurret_Low, int iTurret_High, int iTurret_Distance)
	{
		return _bounding(CMD_TURRET, "", "low="+Integer.toString(iTurret_Low)+",high="+Integer.toString(iTurret_High)+",dist="+Integer.toString(iTurret_Distance));
	}
	
	/////////////////////////////////
	// body...
	static final String CMD_BODY = "body";
	public static String sendBodyInformation(float fBody_Bear, float fBody_Pitch, float fBody_Roll, int iBody_Distance)
	{
		return _bounding(CMD_BODY, "", "bear="+Float.toString(fBody_Bear)+",pitch="+Float.toString(fBody_Pitch)+",roll="+Float.toString(fBody_Roll)+",dist="+Integer.toString(iBody_Distance));
	}

	/////////////////////////////////
	// body...
	static final String CMD_SCAN = "scan";
	public static String sendScan(ScanResult scan)
	{
		return _bounding(CMD_SCAN, "zone", ScanResult.toString(scan));
	}
	
	/////////////////////////////////
	// GPS/Position/Accelerometer...
	static final String CMD_POSITION = "position";
	public static String sendPosition(PositionMarker position)
	{
		return _bounding(CMD_POSITION, "", position.formatTo());
	}

	/////////////////////////////////
	// Camera
	static final String CMD_CAMERA = "camera";
	public static String sendCameraImage(GraphicBuffer buffer)
	{
		String sTemp = "";
		sTemp = "width="+buffer.getWidth()+",height="+buffer.getHeight()+",format=rgb888";
		sTemp += ",data=";
		int iCount = 0;
		for(int i : buffer.getBuffer())
		{
			sTemp += Integer.toHexString(i&0x00ffffff);
			if(i > iCount)
				sTemp+=",";
			iCount++;
		}
		
		return _bounding(CMD_CAMERA, "photo", sTemp);
	}
}
